package com.tchepannou.limbe.gae.converter;


import com.google.appengine.api.datastore.Text;
import com.google.gson.Gson;
import com.tchepannou.util.converter.Converter;

/**
 * User: herve
 * Date: 14-01-15 8:03 PM
 */
public class EmbedConverter implements Converter
{
    private Class _type;
    private Gson _gson = new Gson();

    public EmbedConverter (Class type)
    {
        _type = type;
    }

    @Override
    public Object convert(Object value)
    {
        if (value == null)
        {
            return null;
        }
        else if (value instanceof String)
        {
            return convert((String)value);
        }
        else if (value instanceof Text)
        {
            return convert(((Text)value).getValue());
        }
        else
        {
            return null;
        }
    }

    private Object convert (String value)
    {
        return _gson.fromJson(value, _type);
    }
}
